package com.example.administrator.nurse;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.nurse.NetWork.internetHelper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import zxing.android.CaptureActivity;

/**
 * Created by Administrator on 2016/4/19.
 *
 */

public class common {

    public static final int CodeScan = 0x120;

    public static int phoneWidth = 0;
    //public static String Host = "http://121.42.190.18/ydnurse";
    public static String Host = "http://121.42.190.18";

    public static String diskCachePath = "/sdcard/moblilenurse/";
    public static String nName = "";
    public static String nHeadUrl = "";
    public static String nAccount = "";
    public static String nPw = "";
    public static String nAge = "";
    public static String nSex = "";
    public static String nId = "";
    public static String nIdOfHospital = "";
    public static String nSchool = "";
    public static String nPri = ""; /** 奖项 */

    public static boolean isLogin = false;
    public static Context context = null;

    public static void config(Context mContext,int mPhoneWidth){
        context = mContext;
        phoneWidth = mPhoneWidth;
    }

    public static SharedPreferences getSp(String sName){
        if(context==null){
            return null;
        }
        return sName==null?
                context.getSharedPreferences("user",Context.MODE_APPEND):
                context.getSharedPreferences(sName,Context.MODE_APPEND);
    }

    public static void putStr(String key,String value){
        getSp(null).edit().putString(key,value).commit();
    }

    public static void initUserCache(String sName){
        SharedPreferences sp = sName==null?
                context.getSharedPreferences("user", Context.MODE_APPEND):
                context.getSharedPreferences(sName, Context.MODE_APPEND);
        common.nName = sp.getString("nName","-1");
        common.nId = sp.getString("nId","-1");
        common.nIdOfHospital = sp.getString("nIdOfHospital","-1");
        common.nHeadUrl = sp.getString("nHeadUrl","");
        common.nAccount = sp.getString("nAccount","");
        common.nPw = sp.getString("nPw","");
    }

    public static void saveUserCache(
            String sName,String nAccount,String nPw,String nAge,String nId,
            String nName,String nSchool,String nPri,String nSex,String nHeadUrl
    ){
        SharedPreferences.Editor editor = sName==null?
                context.getSharedPreferences("user",Context.MODE_APPEND).edit():
                context.getSharedPreferences(sName,Context.MODE_APPEND).edit();
        editor.putString("nAccount",nAccount);
        editor.putString("nPw",nPw);
        editor.putString("nAge",nAge);
        editor.putString("nId",nId);
        editor.putString("nName",nName);
        editor.putString("nSchool",nSchool);
        editor.putString("nPri",nPri);
        editor.putString("nSex",nSex);
        editor.putString("nHeadUrl",nHeadUrl);
        editor.commit();
    }

    public static void Toast(String string){
        Toast.makeText(context,string,Toast.LENGTH_SHORT).show();
    }

    /** 扫描 */
    public interface ScanCoderCallBack{
        public void onScanCallBack();
    }

    /** 扫描 公共 function */
    public static void jumpToCoder(Activity activity,ScanCoderCallBack scanCoderCallBack) {

        Toast.makeText(activity, "准备扫码...", Toast.LENGTH_SHORT).show();
        if(scanCoderCallBack!=null){
            scanCoderCallBack.onScanCallBack();
        }else{
            Intent intent = new Intent(activity, CaptureActivity.class);
            activity.startActivityForResult(intent, CodeScan);

        }
        //
    }

    /** 景诺发短信 */
    private void sendMessage(
            String apiUrl,
            String jnAccount,
            String jnPw,
            String phone,
            String msgInfo
    ){
        try {
            final String content = "本次验证码是:"+msgInfo.trim()+"来自【香蕉云】";
            final String urlTemp =
                    apiUrl
                            +"CorpID="+jnAccount
                            +"&Pwd="+jnPw
                            +"&Mobile="+phone.trim()
                            +"&Content="+ URLEncoder.encode(content, "GBK").trim()
                            +"&Cell="+phone.trim()
                            +"&SendTime="
                    ;
            Log.d("zzzzz", "resultJson is " + urlTemp);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String resultJson = internetHelper.getStringFromUrlOnHttpRULconnection(urlTemp);
                        Log.d("zzzzz","sendMessage resultJson is "+resultJson);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void Log(String str){
        Log.d("zzzzz",str);
    }

}
